<%!

  Copyright (c) Facebook, Inc. and its affiliates.

  Licensed under the Apache License, Version 2.0 (the "License");
  you may not use this file except in compliance with the License.
  You may obtain a copy of the License at

      http://www.apache.org/licenses/LICENSE-2.0

  Unless required by applicable law or agreed to in writing, software
  distributed under the License is distributed on an "AS IS" BASIS,
  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  See the License for the specific language governing permissions and
  limitations under the License.

%><% > Autogen%>
#pragma once

<%#program:thrift_includes%>
#include  "<%program:include_prefix%><%program:name%>_fatal_types.h"
<%/program:thrift_includes%>

#include "<%program:include_prefix%><%program:name%>_types.h"
#include "<%program:include_prefix%><%program:name%>_fatal.h"

#include <fatal/type/enum.h>
#include <fatal/type/variant_traits.h>

#include <type_traits>

<% > common/namespace_cpp2_begin%>


<%#program:structs%><%#struct:union?%>
namespace thrift_fatal_impl_detail {

struct <%struct:name%>_Type_enum_traits {
  using type = <% > common/namespace_cpp2%><%struct:name%>::Type;
  using name = thrift_fatal_impl_detail::<% > fatal/fatal_prefix%>__unique_strings_list::Type;

  struct Type__struct_unique_strings_list {
    <%#struct:fields%>
    using <%field:cpp_name%> = thrift_fatal_impl_detail::<% > fatal/fatal_prefix%>__unique_strings_list::<%field:cpp_name%>;
    <%/struct:fields%>
  };

  <%#struct:fields%>
  struct Type__struct_enum_members_<%field:cpp_name%> {
    using name = Type__struct_unique_strings_list::<%field:cpp_name%>;
    using value = std::integral_constant<type, type::<%field:cpp_name%>>;

    <%#field:fatal_annotations?%>
    class annotations {
      struct annotations__unique_annotations_keys {
        <%#field:fatal_annotations%>
        using <%annotation:safe_key%> = thrift_fatal_impl_detail::<% > fatal/fatal_prefix%>__unique_strings_list::<%annotation:safe_key%>;
        <%/field:fatal_annotations%>
      };

      struct annotations__unique_annotations_values {
        <%#field:fatal_annotations%>
        using <%annotation:safe_key%> = <%annotation:fatal_string%>;
        <%/field:fatal_annotations%>
      };

      public:
      using keys = annotations__unique_annotations_keys;
      using values = annotations__unique_annotations_values;
      using map = ::fatal::list<
        <%#field:fatal_annotations%>
        ::apache::thrift::annotation<
          keys::<%annotation:safe_key%>,
          values::<%annotation:safe_key%>
        ><%^last?%>,<%/last?%>
        <%/field:fatal_annotations%>
      >;
    };
    <%/field:fatal_annotations?%>
    <%^field:fatal_annotations?%>
    <% > fatal/fatal_empty_annotation%>
    <%/field:fatal_annotations?%>

  };

  <%/struct:fields%>
  struct Type__struct_enum_members {
    <%#struct:fields%>
    using <%field:cpp_name%> = Type__struct_enum_members_<%field:cpp_name%>;
    <%/struct:fields%>
  };

  using member = Type__struct_enum_members;

  using fields = ::fatal::list<
      <%#struct:fields%>
      member::<%field:cpp_name%><%^last?%>,<%/last?%>
      <%/struct:fields%>
  >;

  <% > fatal/fatal_empty_annotation%>


  static char const *to_string(type e, char const *fallback) {
    switch (e) {
      <%#struct:fields%>
      case type::<%field:cpp_name%>: return "<%field:cpp_name%>";
      <%/struct:fields%>
      default: return fallback;
    }
  }
};

} // thrift_fatal_impl_detail

FATAL_REGISTER_ENUM_TRAITS(
  <% > common/namespace_cpp2%>thrift_fatal_impl_detail::<%struct:name%>_Type_enum_traits,
  ::apache::thrift::detail::type_common_metadata_impl<
    <%program:name%>_tags::module,
    ::apache::thrift::reflected_annotations<<% > common/namespace_cpp2%>thrift_fatal_impl_detail::<%struct:name%>_Type_enum_traits::annotations>,
    static_cast<::apache::thrift::legacy_type_id_t>(0ull)
  >
);

namespace thrift_fatal_impl_detail {

class <%struct:name%>_variant_traits {
  struct <%struct:name%>__struct_unique_identifiers_list {
    <%#struct:fields%>
    using <%field:cpp_name%> = std::integral_constant<<% > common/namespace_cpp2%><%struct:name%>::Type, <% > common/namespace_cpp2%><%struct:name%>::Type::<%field:cpp_name%>>;
    <%/struct:fields%>
  };

  struct <%struct:name%>__struct_unique_getters_list {
    <%#struct:fields%>
    struct <%field:cpp_name%> {
      decltype(auto) operator ()(<%struct:name%> const &variant) const {
        return variant.get_<%field:cpp_name%>();
      }
      decltype(auto) operator ()(<%struct:name%> &variant) const {
        return variant.mutable_<%field:cpp_name%>();
      }
      decltype(auto) operator ()(<%struct:name%> &&variant) const {
        return std::move(variant).move_<%field:cpp_name%>();
      }
    };
    <%^last?%>

    <%/last?%>
    <%/struct:fields%>
  };

  struct <%struct:name%>__struct_unique_setters_list {
    <%#struct:fields%>
    struct <%field:cpp_name%> {
      template <typename... Args>
      decltype(auto) operator ()(<%struct:name%> &variant, Args &&...args) const {
        return variant.set_<%field:cpp_name%>(std::forward<Args>(args)...);
      }
    };
    <%^last?%>

    <%/last?%>
    <%/struct:fields%>
  };

  public:
  using type = <% > common/namespace_cpp2%><%struct:name%>;
  using name = thrift_fatal_impl_detail::<% > fatal/fatal_prefix%>__unique_strings_list::<%struct:name%>;
  using id = type::Type;
  using ids = <%struct:name%>__struct_unique_identifiers_list;
  using descriptors = ::fatal::list<
    <%#struct:fields%>
    ::fatal::variant_member_descriptor<
      <%#field:type%><% > types/type%><%/field:type%>,
      <%struct:name%>__struct_unique_identifiers_list::<%field:cpp_name%>,
      <%struct:name%>__struct_unique_getters_list::<%field:cpp_name%>,
      <%struct:name%>__struct_unique_setters_list::<%field:cpp_name%>,
      ::apache::thrift::reflected_variant_member_metadata<
        thrift_fatal_impl_detail::<% > fatal/fatal_prefix%>__unique_strings_list::<%field:cpp_name%>,
        <%field:key%>,
        <%#field:type%><%type:fatal_type_class%><%/field:type%>
      >
    ><%^last?%>,<%/last?%>
    <%/struct:fields%>
  >;

  <%#struct:fatal_annotations?%>
  class annotations {
    struct annotations__unique_annotations_keys {
      <%#struct:fatal_annotations%>
      using <%annotation:safe_key%> = thrift_fatal_impl_detail::<% > fatal/fatal_prefix%>__unique_strings_list::<%annotation:safe_key%>;
      <%/struct:fatal_annotations%>
    };

    struct annotations__unique_annotations_values {
      <%#struct:fatal_annotations%>
      using <%annotation:safe_key%> = <%annotation:fatal_string%>;
      <%/struct:fatal_annotations%>
    };

    public:
    using keys = annotations__unique_annotations_keys;
    using values = annotations__unique_annotations_values;
    using map = ::fatal::list<
      <%#struct:fatal_annotations%>
      ::apache::thrift::annotation<
        keys::<%annotation:safe_key%>,
        values::<%annotation:safe_key%>
      ><%^last?%>,<%/last?%>
      <%/struct:fatal_annotations%>
    >;
  };
  <%/struct:fatal_annotations?%>
  <%^struct:fatal_annotations?%>
  <% > fatal/fatal_empty_annotation%>
  <%/struct:fatal_annotations?%>


  static id get_id(type const &variant) {
    return variant.getType();
  }

  static bool empty(type const &variant) {
    return variant.getType() == id::__EMPTY__;
  }

  static void clear(type &variant) {
    return variant.__clear();
  }
};

} // thrift_fatal_impl_detail
<%/struct:union?%><%/program:structs%>
<%#program:structs%><%#struct:union?%>
FATAL_REGISTER_VARIANT_TRAITS(
  <% > common/namespace_cpp2%>thrift_fatal_impl_detail::<%struct:name%>_variant_traits,
  ::apache::thrift::detail::type_common_metadata_impl<
    <%program:name%>_tags::module,
    ::apache::thrift::reflected_annotations<<% > common/namespace_cpp2%>thrift_fatal_impl_detail::<%struct:name%>_variant_traits::annotations>,
    static_cast<::apache::thrift::legacy_type_id_t>(<%struct:legacy_type_id%>ull)
  >
);
<%/struct:union?%><%/program:structs%>
<% > common/namespace_cpp2_end%>
